package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.WorkLogTemp;

/**
 * Created by David on 2015/7/24.
 */
public class WorkTempChooseListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<WorkLogTemp> workLogs;
    private View.OnClickListener onWordLogOnClick;
    private View.OnClickListener onAddTemperOnClick;

    /**
     * 构造函数
     *
     * @param context
     * @param workLogs 日志模版列表
     */
    public WorkTempChooseListAdapter(Context context, ArrayList<WorkLogTemp> workLogs) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
        this.workLogs = workLogs;
    }

    @Override
    public int getCount() {
        return workLogs.size();
    }

    @Override
    public Object getItem(int position) {
        return workLogs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_work_log_choose, null);
            holder.tvLogTitle = (TextView) convertView.findViewById(R.id.tv_log_title);
            holder.ivLogIcon = (ImageView) convertView.findViewById(R.id.iv_log_icon);
            holder.tvLogDescription = (TextView) convertView.findViewById(R.id.tv_log_description);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            holder.tvLogDescription.setVisibility(View.GONE);
            holder.tvLogTitle.setText(mContext.getString(R.string.new_work_log_temp));
        } else {
            WorkLogTemp workLog = workLogs.get(position);
            holder.tvLogDescription.setVisibility(View.VISIBLE);
            holder.tvLogDescription.setText(workLog.getDescription());
            holder.tvLogTitle.setText(workLog.getTitle());
        }
        return convertView;
    }

    //刷新数据
    public void refresh(ArrayList<WorkLogTemp> workLogs) {
        this.workLogs = workLogs;
        notifyDataSetChanged();
    }

    /**
     * 设置列表点击事件
     * @param onWordLogOnClick
     * @param onAddTemperOnClick
     */
    public void setOnItemClickListener(View.OnClickListener onWordLogOnClick, View.OnClickListener onAddTemperOnClick) {
        this.onWordLogOnClick = onWordLogOnClick;
        this.onAddTemperOnClick = onAddTemperOnClick;
    }

    //添加日志模板
    public void add(WorkLogTemp workLog) {
        workLogs.add(workLog);
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tvLogTitle;
        ImageView ivLogIcon;
        TextView tvLogDescription;
    }
}